// 漏 Copyright 2009 Aron Granberg
// AstarPath.cs script is licenced under a Creative Commons Attribution-Noncommercial 3.0 Unported License.
// If you want to use the script in commercial projects, please contact me at aron.g@me.com
using UnityEngine;
using System.Collections;
namespace AstarClasses {
[System.Serializable]
public class NodeLink : System.Object {
public Vector3 fromVector;
public Vector3 toVector;
public NodeLink () {
}
}
[System.Serializable]
public class Grid : System.Object {
public string name = "New Grid";
public bool showInEditor = true;
public bool changed = false; //变化
public int startPos = 0; //出发点
public float height = 50; //高度
public int width = 100; //宽
public int depth = 100; //深度
public Vector3 offset = Vector3.zero; //0,0,0
public float nodeSize = 10; //节点大小
public Grid (float h) {
height = h;
width = 100;
depth = 100;
}
public Grid () {
height = 10;
width = 15;
depth = 15;
}
public Grid (Grid o) {
height = o.height;
width = o.width;
depth = o.depth;
offset = o.offset;
nodeSize = o.nodeSize;
}
public bool Contains (Int3 p) {
if (p.x >= offset.x && p.z >= offset.z && p.x < offset.x+(width-1)*nodeSize && p.z < offset.z+(depth-1)*nodeSize && p.y >= offset.y && p.y < offset.y+height) {
return true;
}
return false;
}
public bool Contains (Vector3 p) {
if (p.x >= offset.x && p.z >= offset.z && p.x < offset.x+(width-1)*nodeSize && p.z < offset.z+(depth-1)*nodeSize && p.y >= offset.y && p.y < offset.y+height) {
return true;
}
return false;
}
}
public class Node {
//Global position of the node全球位置节点
public Vector3 vectorPos;
//Local position of the node本地节点的位置
public Int3 pos;
//The previous parent以前的家长
public Node parentx;
//Current parent当前父
public Node parent;
public int invParentDirection = -1;
//Is the node walkable是节点可走
public bool walkable = true;
public int basicCost = 0;
public int extraCost = 0;
private int hx = -1;
//This is the angles to each neighbour这是每个邻居角
public float[] angles = new float[9];
//All neighbours所有的邻居
public Node[] neighbours;
//An array of the direction numbers for all walkable neighbours, see the Scan functions second pass for info about what the numbers mean 一种适合步行的所有邻国的方向数字数组,请参阅扫描功能有关的数字意味着什么第二阶段信息
public int[] neighboursKeys;
//The area the node in is, all other nodes in the same area is accesible from this node 该地区的节点,在同一地区的所有其他节点的accesible从这个节点
public int area = 0;
//When whas the area calculated 当whas的面积计算
public int areaTimeStamp = 0;
//Previous scripts 前脚本
public AstarPath.Path scripty;
//public AstarPath.Path scriptx; 公众AstarPath.Path scriptx;
//Current 当前
public AstarPath.Path script;
public int g {
get {
if (parent == null) {//Does this node have a parent? 这是否有一个父节点?
return 0;
}
//Trace back to the starting point 追溯起点
return basicCost+extraCost+parent.g;
}
}
public int h {
get {
if (script == scripty) {//Has the end changed since last time? 自去年年底的时间改变了?
return hx;
} else {
scripty = script;
hx = Mathf.Abs(script.end.pos.x-pos.x)*10 +
//@Performance, remove the next line if you dont use multiple grids 性能,删除下一行,如果您不使用多个网格
Mathf.Abs((int)AstarPath.active.grids[script.end.pos.y].offset.y-(int)AstarPath.active.grids[pos.y].offset.y)*AstarPath.active.levelCost
+ Mathf.Abs(script.end.pos.z-pos.z)*10;
return hx;
}
}
}
public int f {
get { switch (AstarPath.active.formula) {//@Performance, choose the option you want, but hardcoded is faster性能,选择所需的选项,但速度更快硬
case Formula.HG:
return h+g;
case Formula.H:
return h;
case Formula.G:
return g;
default:
return h+g;
}
}
}
public Node () {
walkable = true;
}
public Node (Node o) {//Copy 副本
walkable = o.walkable;
vectorPos = o.vectorPos;
pos = o.pos;
angles = o.angles;
neighbours = o.neighbours;
}
}
public struct Int3 {
public int x;
public int y;
public int z;
public Int3 (int x,int y,int z) {
this.x = x;
this.y = y;
this.z = z;
}
public Int3 (float x2,float y2) {
x = Mathf.RoundToInt (x2);
y = 0;
z = Mathf.RoundToInt (y2);
}
public Int3 (float x2,float y2,float z2) {
x = Mathf.RoundToInt (x2);
y = Mathf.RoundToInt (y2);
z = Mathf.RoundToInt (z2);
}
public static Int3 operator + (Int3 lhs, Int3 rhs) {
return new Int3 (lhs.x+rhs.x,lhs.y+rhs.y,lhs.z+rhs.z);
}
public static Int3 operator – (Int3 lhs, Int3 rhs) {
return new Int3 (lhs.x-rhs.x,lhs.y-rhs.y,lhs.z-rhs.z);
}
public static bool operator == (Int3 lhs, Int3 rhs) {
return lhs.x==rhs.x&&lhs.y==rhs.y&&lhs.z==rhs.z;
}
public static bool operator != (Int3 lhs, Int3 rhs) {
return lhs.x!=rhs.x||lhs.y!=rhs.y||lhs.z!=rhs.z;
}
public static implicit operator Int3 (Vector3 i)
{
Int3 temp = new Int3 (i.x,i.y,i.z);
return temp;
}
public static implicit operator Vector3 (Int3 i)
{
Vector3 temp = new Vector3 (i.x,i.y,i.z);
return temp;
}
public static implicit operator Vector2 (Int3 i)
{
Vector2 temp = new Vector2 (i.x,i.z);
return temp;
}
public override string ToString () {
return "("+x+","+y+","+z+")";
}
}
public enum IsNeighbour { //邻居
Eight, //八字形
Four //四
}
public enum Formula {
HG,
H,
G
}
public enum PhysicsType { //物理类型
OverlapSphere, //重叠球体
TouchSphere, //接触球体
TouchCapsule, //接触胶囊
Raycast
}
public enum UpDown {
Up,
Down
}
public enum DebugMode {
Areas, //区域
Angles, //角
H,
G
}
public enum Height {
Flat, //平面
Terrain, //地形
Raycast
}
public enum Simplify {
None,
Simple, //简单的
Full
}
}